package ru.spb.osll.myfit.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.client.widget.IStyleConstants;
import ru.spb.osll.myfit.common.ExecutingResult;
import ru.spb.osll.myfit.common.TextParsingTools;
import ru.spb.osll.myfit.common.chart.ChartTree;
import ru.spb.osll.myfit.common.chart.ChartTreeNode;
import ru.spb.osll.myfit.common.chart.Launch;

import java.util.HashSet;
import java.util.Set;

public class ChartsView extends AbstractView {
  private final Widget m_titleLabel;
  private final VerticalPanel m_contentPanel = new VerticalPanel();
  private Set<String> m_chosenCharts = new HashSet<String>();

  public ChartsView() {
    m_titleLabel = createTitlePanel();

    createRootPanel();
  }

  private Widget createTitlePanel() {
    HorizontalPanel panel = new HorizontalPanel();
    panel.setStyleName(IStyleConstants.TITLE_PANEL_STYLE_NAME);
    Label prefix = new Label("Chart: ");
    prefix.addStyleName(IStyleConstants.PAGE_TITLE_LABEL_STYLE_NAME);
    panel.add(prefix);
    panel.setCellWidth(prefix, "auto");

    HTML title = new HTML("Charts");
    title.addStyleName(IStyleConstants.PAGE_TITLE_STYLE_NAME);
    panel.add(title);
    panel.setCellWidth(title, "100%");

    return panel;
  }

  @Override
  protected Widget createContentPanel() {
    return m_contentPanel;
  }

  @Override
  protected Widget createEditPanel() {
    VerticalPanel editPanel = new VerticalPanel();
    editPanel.setStyleName(IStyleConstants.MYFIT_VIEW_TOOL_PANEL_STYLE_NAME);
    editPanel.add(m_titleLabel);
    editPanel.add(createButtonPanel());
    return editPanel;
  }

  private Widget createButtonPanel() {
    Panel buttonPanel = new HorizontalPanel();

    return buttonPanel;
  }

  public void display(Set<ExecutingResult> results) {
    for (ExecutingResult result : results) {
      CheckBox checkBox = new CheckBox(result.getId());
      m_contentPanel.add(checkBox);
    }

    Button button = new Button("Compare", new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        StringBuilder ids = new StringBuilder(ViewConstants.CHART_COMPARISON_ADDRESS + "?");

        for (Object widget : m_contentPanel) {
          if (widget instanceof CheckBox) {
            if (((CheckBox) widget).getValue()) {
              ids.append("id=");
              ids.append(((CheckBox) widget).getText());
              ids.append("&");
            }
          }
        }

        Window.Location.assign(ids.toString());
      }
    });

    m_contentPanel.add(button);
  }

  public void display(ChartTree result) {
    Tree tree = new Tree();

    for (ChartTreeNode topLevelNode : result.getRoot().getChildren()) {
      TreeItem item = new TreeItem(topLevelNode.getName());
      fillItem(item, topLevelNode);
      tree.addItem(item);
    }

    m_contentPanel.add(tree);

    Button button = new Button("Compare", new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        StringBuilder ids = new StringBuilder(ViewConstants.CHART_COMPARISON_ADDRESS + "?");
        for (String id : m_chosenCharts) {
          ids.append("id=");
          ids.append(id);
          ids.append("&");
        }
        Window.Location.assign(ids.toString());
      }
    });

    m_contentPanel.add(button);
  }

  private void fillItem(TreeItem item, ChartTreeNode node) {
    for (ChartTreeNode child : node.getChildren()) {
      TreeItem newItem = item.addItem(child.getName());
      fillItem(newItem, child);
    }
    for (final Launch launch : node.getLaunches()) {
      TreeItem launchItem = item.addItem(launch.getDate().toString());
      for (final String name : launch.getChartNames()) {
        CheckBox checkBox = new CheckBox(name);
        checkBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
          @Override
          public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
            if (booleanValueChangeEvent.getValue()) {
              m_chosenCharts.add(new String(launch.getId() + TextParsingTools.TABLE_CELL_SEPARATOR + name));
            } else {
              m_chosenCharts.remove(new String(launch.getId() + TextParsingTools.TABLE_CELL_SEPARATOR + name));
            }
          }
        });
        launchItem.addItem(checkBox);
      }
    }
  }
}
